// getter.js


function parseNameList(namepath, sep) {
    if (namepath == null) {
        return []
    }
    if (sep == null) {
        sep = '/'
    }
    let str = namepath + ""
    return str.split(sep)
}

function getTargetFromObject(obj, namelist) {
    let p = obj;
    for (let i in namelist) {
        if (p == null) { return null }
        let name = namelist[i]
        if ((name == null) || (name == "")) {
            continue
        }
        p = p[name]
    }
    return p
}


////////////////////////////////////////////////////////////////////////////////

function Getter(o) {
    this.obj = o
}

Getter.prototype = {
    Get(namepath, sep, defaultValue) {
        let namelist = parseNameList(namepath, sep)
        let target = getTargetFromObject(this.obj, namelist)
        if (target == null) { target = defaultValue }
        return target
    }
}

function NewGetter(obj) {
    return new Getter(obj)
}

function Test() {
    let o1 = {
        a: {
            b: {
                c: [
                    "u", "v", "w", "x", "y"
                ]
            }
        }
    }
    let getter = NewGetter(o1)
    let o2 = getter.Get('/a/b/c/3', '/', false)
    console.log(o2)
}


export default {
    NewGetter, Test
}

